package vn.cuhanh.viewpager;

import vn.cuhanh.adapter.InfinitePagerAdapter;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

/**
 * A {@link ViewPager} that allows pseudo-infinite paging with a wrap-around
 * effect. Should be used with an {@link InfinitePagerAdapter}.
 * 
 */
public class InfiniteViewPager extends ViewPager {
	private Runnable onClickCallback;

	public InfiniteViewPager(Context context) {
		super(context);
	}

	public InfiniteViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public void setAdapter(PagerAdapter adapter) {
		super.setAdapter(adapter);
		// offset first element so that we can scroll to the left
		setCurrentItem(0);
	}

	@Override
	public void setCurrentItem(int item) {
		// offset the current item to ensure there is space to scroll
		item = getOffsetAmount() + (item % getAdapter().getCount());
		super.setCurrentItem(item);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent event) {
//		this.requestDisallowInterceptTouchEvent(true);
		boolean result = super.onInterceptTouchEvent(event);
		return result;
	}

	private int getOffsetAmount() {
		if (getAdapter() instanceof InfinitePagerAdapter) {
			InfinitePagerAdapter infAdapter = (InfinitePagerAdapter) getAdapter();

			return infAdapter.getRealCount() * 100;
		} else {
			return 0;
		}
	}

	public Runnable getOnClickCallback() {
		return onClickCallback;
	}

	public void setOnClickCallback(Runnable onClickCallback) {
		this.onClickCallback = onClickCallback;
	}

}